﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversalDataAccess.BusinessEntities;

namespace UniversalDataAccess.DataAccess
{
    public class LaboratoriesDataAccess : ResearchDivisionsDataAccess
    {
        public LaboratoriesDataAccess(String dbName, String conn, Boolean usePredefinedMetadata)
            : base(dbName, conn, usePredefinedMetadata)
        {
        }

        public List<Laboratory> SelectLaboratoriesList()
        {
            List<ResearchDivision> divList = SelectResearchDivisionsList();

            List<LABORATORY> labList = dataContext.LABORATORY.ToList();

            //Merge result
            var res = (from div in divList
                       join lab in labList on div.ID equals lab.ID
                       select new Laboratory
                       {
                           DataBaseName = dataBaseName,
                           CLASS_ID = div.CLASS_ID,
                           EXT_ID = div.EXT_ID,
                           ID = div.ID,
                           NAME = div.NAME,
                           PARENT_ID = div.PARENT_ID,
                           BOSS_ID = div.BOSS_ID,
                           DATE_CREATED = div.DATE_CREATED,
                           DESCRIPTION = div.DESCRIPTION,
                           FINANCING = (Decimal)div.FINANCING,
                           PURPOSE = div.PURPOSE,
                           SPONSORS = div.SPONSORS,
                           AWARDS = div.AWARDS,
                           WORKING_HOURS = lab.WORKING_HOURS
                       }).ToList();

            return res;
        }

        public Laboratory SelectLaboratoryBy_Id(int Id)
        {
            try
            {
                ResearchDivision basePart = SelectResearchDivisionBy_Id(Id);

                List<LABORATORY> laboratoryPart = dataContext.LABORATORY.ToList();

                //Merge result
                var res = (from lab in laboratoryPart
                           where lab.ID == basePart.ID
                           select new Laboratory
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = basePart.CLASS_ID,
                               EXT_ID = basePart.EXT_ID,
                               ID = basePart.ID,
                               NAME = basePart.NAME,
                               PARENT_ID = basePart.PARENT_ID,
                               BOSS_ID = basePart.BOSS_ID,
                               DATE_CREATED = basePart.DATE_CREATED,
                               DESCRIPTION = basePart.DESCRIPTION,
                               FINANCING = (Decimal)basePart.FINANCING,
                               PURPOSE = basePart.PURPOSE,
                               SPONSORS = basePart.SPONSORS,
                               AWARDS = basePart.AWARDS,
                               WORKING_HOURS = lab.WORKING_HOURS
                           }).FirstOrDefault();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectLaboratoryById");
            }
        }

        public void InsertLaboratory(Laboratory laboratory, ref int generated_Id)
        {
            try
            {
                InsertResearchDivision(laboratory, ref generated_Id);

                int new_Id = generated_Id;
                LABORATORY laboratoryPart = new LABORATORY();
                laboratoryPart.RESEARCH_DIVISION = dataContext.RESEARCH_DIVISION.Where(t => t.ID == new_Id).FirstOrDefault();
                laboratoryPart.ID = new_Id;
                laboratoryPart.WORKING_HOURS = laboratory.WORKING_HOURS;

                //Submit changes
                dataContext.AddToLABORATORY(laboratoryPart);
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while InsertLaboratory");
            }
        }

        public void UpdateLaboratory(Laboratory laboratory)
        {
            try
            {
                UpdateResearchDivision(laboratory);

                LABORATORY laboratoryPart = dataContext.LABORATORY.Where(t => t.ID == laboratory.ID).FirstOrDefault();

                laboratoryPart.WORKING_HOURS = laboratory.WORKING_HOURS;

                //Submit changes
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while UpdateLaboratory");
            }
        }

        public void DeleteLaboratory(Laboratory laboratory)
        {
            try
            {
                LABORATORY labPart = dataContext.LABORATORY.Where(t => t.ID == laboratory.ID).FirstOrDefault();

                dataContext.DeleteObject(labPart);
                dataContext.SaveChanges();


                DeleteResearchDivision(laboratory);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while DeleteLaboratory");
            }
        }
    }
}
